﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;
public partial class Admin_ThanhPhoThem : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    void LoadMaNuoc()
    {
        ddlNuoc.DataValueField = "Id";
        ddlNuoc.DataTextField = "TenNuoc";
        ddlNuoc.DataSource = db.Nuocs;
        ddlNuoc.DataBind();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMaNuoc();
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        string ten = txtTen.Text.Trim();
        string ma = ddlNuoc.SelectedValue;
        string dgia = txtDonGia.Text.Trim();
        if (txtTen.Text == "")
            return;
        int d = db.ThanhPhoes.Count(p => p.TenThanhPho == ten);
        if (d == 0)
        {
            ThanhPho lsp = new ThanhPho();
            lsp.TenThanhPho = ten;
            lsp.DonGia = int.Parse(dgia);
            lsp.IdNuoc = Int32.Parse(ma);

            db.ThanhPhoes.AddObject(lsp);
            db.SaveChanges();
            lbThongBao.Text = "Thêm thành công!";

        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên thành phố đã có rồi!');", true);
        }
    }
}